<?php
function desire_register_settings() {
    register_setting('desire_options','desire_options','desire_validate_input');
}
add_action('admin_init','desire_register_settings');

/* Populating drop down items */
$bloglayoutlist = array(
    'traditional' => array(
        'value' => 'traditional',
        'label' => __('Traditional','desire')
    ),
    'onemag' => array(
        'value' => 'onemag',
        'label' => __('One Column Magazine','desire')
    ),
    'twomag' => array(
        'value' => 'twomag',
        'label' => __('Two Column Magazine','desire')
    )
);

$titlefontlist = array(
    'default' => array(
        'value' => 'default',
        'label' => __('Default','desire')
    ),
    'Kristi' => array(
        'value' => 'Kristi',
        'label' => __('Kristi','desire')
    ),
    'Damion' => array(
        'value' => 'Damion',
        'label' => __('Damion','desire')
    ),
    'Vibur' => array(
        'value' => 'Vibur',
        'label' => __('Vibur','desire')
    )
);

$bodyfontlist = array(
    'Helvetica, Arial, sans-serif' => array(
        'value' => 'Helvetica, Arial, sans-serif',
        'label' => __('Helvetica, Arial','desire')
    ),
    'Times New Roman, serif' => array(
        'value' => 'Times New Roman, serif',
        'label' => __('Times New Roman','desire')
    ),
    'Verdana, sans-serif' => array(
        'value' => 'Verdana, sans-serif',
        'label' => __('Verdana','desire')
    )
);

function desire_admin_styles() {
    wp_register_style('desire-admin-css', get_template_directory_uri().'/style-admin.css', array(), '1.2', 'all');
    wp_enqueue_style('desire-admin-css');
}

function desire_theme_options() {
    $adminpage = add_theme_page(__('Desire Theme Options','desire'), __('Desire Theme Options','desire'), 'edit_theme_options', 'theme_options', 'desire_options_page');
    add_action('admin_print_styles-' . $adminpage, 'desire_admin_styles');
}
add_action('admin_menu', 'desire_theme_options');

function desire_options_page() {
    global $desireoptions, $desirelogo, $bloglayoutlist, $titlefontlist, $bodyfontlist;
    $message = "";
    $uploadmessage = "";
    
    /* Checking if form is just submitted */
    if (!isset($_REQUEST['settings-updated']))
        $_REQUEST['settings-updated'] = false;
?>
<div class="wrap">
    <?php screen_icon(); echo '<h2 class="admin-title">'.ucwords(get_current_theme()).__( ' Theme Settings','desire' ).'</h2>'; ?>
    <?php if( false !== $_REQUEST['settings-updated'] ) : ?>
        <?php $message = '<div class="success">'.__('Settings have been saved','desire').'</div>'; ?>
    <?php endif; ?>
    <?php if(isset($_POST['desire_reset_options'])) {
        delete_option('desire_options');
        $desireoptions = desire_init_options();
        $message = '<div class="success">'.__('Default settings restored','desire').'</div>';
    } ?>
    <?php echo $message; ?>
    <h3 class="section-title"><?php _e('Like this theme ? Feel free to donate','desire'); ?></h3>
    <form action="https://www.paypal.com/cgi-bin/webscr" method="post">
        <table class="section-table">
        <tr>
            <td>
                <p><?php _e('This theme is created and maintained by','desire'); ?> <a href="http://www.nischalmaniar.info">Nischal Maniar</a></p>
                <input type="hidden" name="cmd" value="_s-xclick">
                <input type="hidden" name="hosted_button_id" value="T7SUJQQE2ANMS">
                <input type="image" src="https://www.paypalobjects.com/WEBSCR-640-20110429-1/en_US/i/btn/btn_donate_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
                <img alt="" border="0" src="https://www.paypalobjects.com/WEBSCR-640-20110429-1/en_US/i/scr/pixel.gif" width="1" height="1">
            </td>
        </tr>
        </table>
    </form>
    <form method="post" action="options.php">
        <?php settings_fields('desire_options'); ?>
        <h3 class="section-title"><?php _e('Display Options','desire'); ?></h3>
        <table class="section-table">
            <tr>
                <td><label><?php _e('Choose font for this theme','desire'); ?></label></td>
                <td>
                    <select name="desire_options[body_font]">
                        <?php
                            foreach($bodyfontlist as $bodyfont) { ?>
                                <option value="<?php echo esc_attr($bodyfont['value']); ?>" <?php selected($desireoptions['body_font'], $bodyfont['value']); ?>><?php echo $bodyfont['label']; ?></option>   
                        <?php
                            }
                        ?>
                    </select>
                </td>
            </tr>
            <tr>
                <td><label><?php _e('Enable Custom Background','desire'); ?></label></td>
                <td>
                    <input type="checkbox" name="desire_options[enable_custom_background]" value="true" <?php checked(true,$desireoptions['enable_custom_background']); ?> />
                    <span style="margin-left: 15px;"><i><?php _e('Custom Background settings are enabled on the side panel','desire'); ?></i></span>
                </td>
            </tr>
            <tr>
                <td><label><?php _e('Enable Post Slideshow on front page','desire'); ?></label></td>
                <td>
                    <input type="checkbox" name="desire_options[enable_post_slideshow]" value="true" <?php checked(true,$desireoptions['enable_post_slideshow']); ?> />                    
                </td>
            </tr>
            <tr>
                <td><label><?php _e('Enter the number of post entries in the slideshow','desire'); ?></label></td>
                <td>
                    <input type="text" name="desire_options[slideshow_count]" value="<?php esc_attr_e($desireoptions['slideshow_count']); ?>" />
                    <p><i><?php _e('Minimum should be 2. Default count is 5','desire'); ?></i></p>
                </td>
            </tr>
        </table>
        
        <h3 class="section-title"><?php _e('Header Options','desire'); ?></h3>
        <table class="section-table">
            <tr>
                <td><label><?php _e('Enable Custom Header','desire'); ?></label></td>
                <td>
                    <input type="checkbox" name="desire_options[enable_custom_header]" value="true" <?php checked(true,$desireoptions['enable_custom_header']); ?> />
                    <span style="margin-left: 15px;"><i><?php _e('Custom Header settings are enabled on the side panel','desire'); ?></i></span>
                </td>
            </tr>
            <tr>
                <td><label><?php _e('Custom google font for Site Title','desire'); ?></label></td>
                <td>
                    <select name="desire_options[site_title_font]">
                        <?php
                            foreach($titlefontlist as $titlefont) { ?>
                                <option value="<?php echo esc_attr($titlefont['value']); ?>" <?php selected($desireoptions['site_title_font'], $titlefont['value']); ?>><?php echo $titlefont['label']; ?></option>   
                        <?php
                            }
                        ?>
                    </select>
                </td>
            </tr>
            <tr>
                <td><label><?php _e('Enable Breadcrumbs','desire'); ?></label></td>
                <td>
                    <input type="checkbox" name="desire_options[enable_breadcrumbs]" value="true" <?php checked(true,$desireoptions['enable_breadcrumbs']); ?> />
                </td>
            </tr>
        </table>
        
        <h3 class="section-title"><?php _e('Blog Options','desire'); ?></h3>
        <table class="section-table">
            <tr>
                <td><label><?php _e('Blog layout options','desire'); ?></label></td>
                <td>
                    <select name="desire_options[blog_layout]">
                        <?php
                            foreach($bloglayoutlist as $bloglayout) { ?>
                                <option value="<?php echo esc_attr($bloglayout['value']); ?>" <?php selected($desireoptions['blog_layout'], $bloglayout['value']); ?>><?php echo $bloglayout['label']; ?></option>   
                        <?php
                            }
                        ?>
                    </select>
                    <p><i><?php _e('Single post and Page will always have traditional layout','desire'); ?></i></p>
                </td>
            </tr>
            <tr>
                <td><label><?php _e('Display categories for:','desire'); ?></label></td>
                <td>
                    <input type="checkbox" name="desire_options[show_category_front]" value="true" <?php checked(true,$desireoptions['show_category_front']); ?> /><span style="margin-left: 5px;"><?php _e('Front page posts, Archive posts, Search results','desire'); ?></span>
                    <input style="margin-left: 20px;" type="checkbox" name="desire_options[show_category_single]" value="true" <?php checked(true,$desireoptions['show_category_single']); ?> /><span style="margin-left: 5px;"><?php _e('Single post','desire'); ?></span>
                </td>
            </tr>
            <tr>
                <td><label><?php _e('Display tags for:','desire'); ?></label></td>
                <td>
                    <input type="checkbox" name="desire_options[show_tags_front]" value="true" <?php checked(true,$desireoptions['show_tags_front']); ?> /><span style="margin-left: 5px;"><?php _e('Front page posts, Archive posts, Search results','desire'); ?></span>
                    <input style="margin-left: 20px;" type="checkbox" name="desire_options[show_tags_single]" value="true" <?php checked(true,$desireoptions['show_tags_single']); ?> /><span style="margin-left: 5px;"><?php _e('Single post','desire'); ?></span>
                </td>
            </tr>
        </table>
        
        <h3 class="section-title"><?php _e('Sidebar Options','desire'); ?></h3>
        <table class="section-table">
            <tr>
                <td><label><?php _e('Enter the width of the sidebar','desire'); ?></label></td>
                <td>
                    <input type="text" name="desire_options[sidebar_width]" value="<?php esc_attr_e($desireoptions['sidebar_width']); ?>" />
                    <p><i><?php _e('Minimum width is 200px and maximum allowed is 400px. Default width is 250px','desire'); ?></i></p>
                </td>
            </tr>
            <tr>
                <td><label><?php _e('Adsense area code','desire'); ?></label></td>
                <td>
                    <textarea name="desire_options[adsense_code]" rows="5" cols="20"><?php esc_attr_e($desireoptions['adsense_code']); ?></textarea>
                    <p><i><?php _e('You might have to style the HTML code in the adsense code your provide to fit in the sidebar','desire'); ?></i></p>
                </td>
            </tr>
            <tr>
                <td><label><?php _e('Show tabbed content in sidebar','desire'); ?></label></td>
                <td>
                    <input type="checkbox" name="desire_options[show_tabbed_content]" value="true" <?php checked(true,$desireoptions['show_tabbed_content']); ?> />
                </td>
            </tr>
        </table>
        <br />
        <input type="submit" class="button-primary" value="<?php _e('Save settings','desire'); ?>" />
    </form>
    <form method="post">
        <input name="desire_reset_options" id="desire_reset_options" type="submit" class="button-secondary" value="<?php _e('Reset settings','desire'); ?>" />
    </form>
</div>
<br /><br />
<div class="wrap" id="desire-upload-logo-wrap">
    <?php
        if(isset($_POST['desire_upload_submit'])) {
            $uploadmessage = desire_upload_logo($_FILES['desire_logo']);
        } else if(isset($_POST['desire_upload_reset'])) {
            delete_option('desire_logo');
            $uploadmessage = '<div class="success">'.__('Default logo restored','desire').'</div>';
            desire_init_logo();
        } else if(isset($_POST['desire_upload_remove'])) {
            update_option('desire_logo','none');
            $uploadmessage = '<div class="success">'.__('Logo removed','desire').'</div>';
            $desirelogo = get_option('desire_logo');
        }        
    ?>
    <?php screen_icon(); echo '<h2 class="admin-title">'.ucwords(get_current_theme()).__( ' Theme Logo Settings','desire' ).'</h2>'; ?>
    <?php echo $uploadmessage; ?>
    <form id="desireuploadlogoform" style="margin-top: 15px;" name="desireuploadlogoform" method="post" enctype="multipart/form-data" action="#desire-upload-logo-wrap">
        <table class="section-table">
            <tr>
                <td>
                    <label><?php _e('Upload Custom Logo','desire'); ?></label>                    
                </td>
                <td>
                    <p><input type="file" name="desire_logo" /></p>
                    <p><i><?php _e('Maximum upload size is ','desire'); ?> <?php echo DESIRE_MAX_UPLOAD_SIZE; ?>KB. <?php _e('Please upload a valid jpg, jpeg, gif or png file','desire'); ?></i></p>
                    <p><?php if($desirelogo != 'none'): ?><img style="vertical-align: top;" src="<?php echo $desirelogo; ?>" /><?php endif; ?></p>
                </td>
            </tr>
        </table>
        <input type="submit" name="desire_upload_submit" id="desire_upload_submit" class="button-primary" value="<?php _e('Upload Logo','desire'); ?>" />
        <input name="desire_upload_reset" id="desire_upload_reset" type="submit" class="button-secondary" value="<?php _e('Restore default logo','desire'); ?>" />
        <input name="desire_upload_remove" id="desire_upload_remove" type="submit" class="button-secondary" value="<?php _e('Remove logo','desire'); ?>" />
    </form>
</div>
<br /><br />
<?php
    }
    
function desire_validate_input($input) {
    $input['sidebar_width'] = wp_filter_nohtml_kses( $input['sidebar_width'] );
    $input['sidebar_width'] = intval($input['sidebar_width']);
    if($input['sidebar_width'] < 200) $input['sidebar_width'] = 200;
    else if($input['sidebar_width'] > 400) $input['sidebar_width'] = 400;
    
    $input['slideshow_count'] = wp_filter_nohtml_kses( $input['slideshow_count'] );
    $input['slideshow_count'] = intval($input['slideshow_count']);
    if($input['slideshow_count'] < 2) $input['slideshow_count'] = 2;
    
    $chkboxinputs = array('enable_custom_header','enable_custom_background','enable_post_slideshow','enable_breadcrumbs','show_tabbed_content','show_category_front','show_category_single','show_tags_front','show_tags_single');
    foreach($chkboxinputs as $chkbox) {
        if (!isset($input[$chkbox]))
		$input[$chkbox] = null;
	$input[$chkbox] = ( $input[$chkbox] == true ? true : false );
    }
    return $input;
}
?>